package com.jy.api.domain;

import com.jy.api.util.Md5Util;
import com.jy.api.util.UuidUtil;
import lombok.Data;
import lombok.NoArgsConstructor;

import javax.persistence.*;
import java.util.Date;

/**
 * 优惠券实例
 * <p>
 * Created by hanxi on 2017/11/16.
 */
@Entity
@Data
@NoArgsConstructor
public class CouponInstance {

    @Id
    @Column(length = 50)
    private String id;


    private String storeId;

    /**
     * 发卷人ID
     */
    private String creatorId;

    private int spec;
    /**
     * 发卷人姓名
     */
    private String creatorName;

    /**
     * 优惠券实例编码
     */
    private String code;
    /**
     * 优惠券id
     */
    private String couponId;
    /**
     * 领取人id
     */
    private String ownerId;
    /**
     * 领取人open id
     */
    private String openId;
    /**
     * 起用时间
     */
    private Date beginTime;
    /**
     * 结束时间
     */
    private Date endTime;
    /**
     * 核销时间
     */
    private Date validateTime;

    /**
     * 状态
     */
    @Enumerated(EnumType.STRING)
    private CouponInstanceStatus stauts;
    /**
     * 关联优惠券代码，比如分享自优惠券xxx
     */
    private String refCouponCode;

    @Column(insertable = false, updatable = false)
    @Temporal(TemporalType.TIMESTAMP)
    private Date addTime;
    @Column(insertable = false, updatable = false)
    @Temporal(TemporalType.TIMESTAMP)
    private Date updateTime;

    private String orderId;
    /**
     * 优惠卷发放
     *
     * @param coupon
     * @param member
     * @param creatorId
     * @param creatorName
     * @return
     */
    public void addCouponInstance(Coupon coupon, Member member, String creatorId, String creatorName) {
        id = UuidUtil.getUuid();
        this.creatorId = creatorId;
        this.creatorName = creatorName;
        this.code = Md5Util.encrypt16(id);
        couponId = coupon.getId();
        ownerId = member.getId();
        storeId = coupon.getStoreId();
        openId = member.getOpenId();
        beginTime = coupon.getStartTime();
        endTime = coupon.getEndTime();
        stauts = CouponInstanceStatus.NORMAL;
    }


    /**
     * 分享优惠卷领取优惠卷发放
     *
     * @param coupon
     * @param couponInstanceId
     * @return
     */
    public CouponInstance(Coupon coupon, Member member, String couponInstanceId) {
        id = UuidUtil.getUuid();
        creatorName = "分享领取";
        ownerId = member.getId();
        openId = member.getOpenId();
        this.code = Md5Util.encrypt16(id);
        couponId = coupon.getId();
        storeId = coupon.getStoreId();
        beginTime = coupon.getStartTime();
        endTime = coupon.getEndTime();
        stauts = CouponInstanceStatus.NORMAL;
        refCouponCode = couponInstanceId;
    }


    /**
     * 优惠卷发放
     *
     * @param coupon
     * @param employee
     * @return
     */
    public void addCouponInstance(Coupon coupon, Employee employee) {
        id = UuidUtil.getUuid();
        creatorId = employee.getUsername();
        creatorName = employee.getName();
        this.code = Md5Util.encrypt16(id);
        couponId = coupon.getId();
        storeId = coupon.getStoreId();
        beginTime = coupon.getStartTime();
        endTime = coupon.getEndTime();
        stauts = CouponInstanceStatus.CREATED;
    }

//    /**
//     * 校验优惠卷状态
//     *
//     * @param memberId
//     * @return
//     */
//    public String checkStatus(String memberId) {
//        String msg = null;
//        if (!StringUtils.equals(memberId, ownerId)) {
//            msg = "优惠卷使用失败！订单未与该会员绑定！";
//        } else if (stauts == CouponInstanceStatus.NOTSURE) {
//            msg = "优惠卷使用失败！优惠卷核销中！";
//        } else if (stauts == CouponInstanceStatus.EXPIRED) {
//            msg = "优惠卷使用失败！优惠卷已过期！";
//        } else if (stauts == CouponInstanceStatus.VERIFIED) {
//            msg = "优惠卷使用失败！优惠卷已核销！";
//        }
//        return msg;
//    }

}
